package com.ourway.controller;

import com.liuweiheng.base.entity.R;
import com.liuweiheng.base.utils.HttpUtil;
import com.ourway.constant.FeiShuConstants;
import com.ourway.service.IFeiShuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * 飞书API控制器
 *
 * @author lwh
 * @date 2021/11/22
 */
@RestController
@RequestMapping("/feishu/sys")
@Slf4j
public class FeiShuSystemController {

    private final IFeiShuService IFeiShuService;

    private final FeiShuConstants feiShuConstants;

    public FeiShuSystemController(IFeiShuService IFeiShuService, FeiShuConstants feiShuConstants) {
        this.IFeiShuService = IFeiShuService;
        this.feiShuConstants = feiShuConstants;
    }

    /**
     * 获取tenant_access_token
     */
    @GetMapping("/getTenantAccessToken")
    public R getTenantAccessToken() {
        // 获取 tenant_access_token
        return R.ok(IFeiShuService.getTenantAccessToken());
    }

    /**
     * 获取 FeiShu 第三方登录重定向地址
     */
    @GetMapping("/getLoginRedirectUrl")
    public R getLoginRedirectUrl() throws UnsupportedEncodingException {
        String url = IFeiShuService.getLoginRedirectUrl();
        return R.ok().putData(url);
    }

    /**
     * 获取IP地址
     * @param request HttpServletRequest
     * @return IP地址
     */
    @GetMapping("/getIp")
    public R getIp(HttpServletRequest request) {
        return R.ok().putData(HttpUtil.getIpAddress(request));
    }

}
